﻿using System.Collections.Generic;
using KMF.Common;

namespace KMF.DAL
{
    public partial class ProductCateTempMappingDb
    {
        /// <summary>
        /// Lấy về danh sách bản ghi theo ProductCateId
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public static List<ViewProductCateTempMappingDto> GetByCateId(string cateId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_ProductCateTempMapping_GetByCateId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@CateId", cateId));
                return dao.GetDTOList<ViewProductCateTempMappingDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static int Add(ProductCateTempMappingDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_ProductCateTempMapping_Add");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@TemplateId", model.TemplateId));
                command.Parameters.Add(dao.CreateParameter("@ProductCateId", model.ProductCateId));
                command.Parameters.Add(dao.CreateParameter("@IsOnHomePage", model.IsOnHomePage));
                command.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                return (int) command.ExecuteScalar();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static ViewProductCateTempMappingDto GetById(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_ProductCateTempMapping_GetByIdJoin");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return dao.GetSingleDTO<ViewProductCateTempMappingDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static ProductCateTempMappingDto GetSingleById(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_ProductCateTempMapping_GetById");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return dao.GetSingleDTO<ProductCateTempMappingDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static int Delete(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_ProductCateTempMapping_Delete");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static int Update(ProductCateTempMappingDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_ProductCateTempMapping_Update");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", model.Id));
                command.Parameters.Add(dao.CreateParameter("@TemplateId", model.TemplateId));
                command.Parameters.Add(dao.CreateParameter("@ProductCateId", model.ProductCateId));
                command.Parameters.Add(dao.CreateParameter("@IsOnHomePage", model.IsOnHomePage));
                command.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<ProductCateTempMappingDto> GetAllByCateId(string categoryId)
        {
            var dao = SharedSqlDao;
            var commnad = dao.GetDbSprocCommand("WSPX_Admin_ProductCateTempMapping_GetAllByCateId");
            try
            {
                commnad.Parameters.Add(dao.CreateParameter("@CategoryId", categoryId));
                return dao.GetDTOList<ProductCateTempMappingDto>(ref commnad);
            }
            finally
            {
                commnad.Connection.Close();
            }
        }
    }
}
